package com.hubery.designpattern.design.strategy;

import com.hubery.designpattern.enums.MessagePlatformTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lixueyang
 * @since 2023/2/28 12:02
 */
@Slf4j
@Component
public class SendMessageStrategySupport {

    @Resource
    private List<SendMessageStrategy> sendMessageStrategyList;

    private Map<MessagePlatformTypeEnum, SendMessageStrategy> sendMessageStrategyMap = new EnumMap<>(MessagePlatformTypeEnum.class);

    @PostConstruct
    public void init() {
        sendMessageStrategyMap = sendMessageStrategyList.stream().collect(Collectors.toMap(SendMessageStrategy::getTypeEnum, Function.identity()));
    }

    public SendMessageStrategy getSendMessageStrategyService(MessagePlatformTypeEnum messagePlatformTypeEnum) {
        return sendMessageStrategyMap.getOrDefault(messagePlatformTypeEnum, null);
    }
}
